// Post.m
//
// Copyright (c) 2012 Mattt Thompson (http://mattt.me/)
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

#import "Post.h"
#import "User.h"

#import "LCYHttpTool.h"

@implementation Post

- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.postID = (NSUInteger)[[attributes valueForKeyPath:@"id"] integerValue];
    self.text = [attributes valueForKeyPath:@"name"];//text
    
//    self.user = [[User alloc] initWithAttributes:[attributes valueForKeyPath:@"user"]];
    
    return self;
}

#pragma mark -
+ (void)setupUserInfoWithBlock:(void (^)(NSArray *posts, NSError *error))block
{
    //2.发送网络请求  stream/0/posts/stream/global   aroundme/hot.json
    NSMutableDictionary *_getArguments;
    
    CGFloat _latitude = [@"30.2802105" floatValue];
    CGFloat _longitude= [@"120.121834" floatValue];
    NSInteger _accuracy = 5;
    NSInteger _page = 1;
    NSInteger _pageSize = 20;
    _getArguments = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                         [NSString stringWithFormat:@"%f",_latitude], @"lat",
                         [NSString stringWithFormat:@"%f",_longitude], @"lng",
                         [NSString stringWithFormat:@"%d",_accuracy], @"accuracy",
                         [NSString stringWithFormat:@"%d",_page], @"page",
                         [NSString stringWithFormat:@"%d",_pageSize], @"pcount",
                         nil];

    //aroundme/hot.json 讨论
    [gHttpToolsMgr get:@"aroundme/hot.json" params:_getArguments success:^(NSArray *responseObj) {
        
//        NSArray *postsFromResponse = [responseObj valueForKeyPath:@"data"];
        NSArray *postsFromResponsse = responseObj;

        NSMutableArray *mutablePosts = [NSMutableArray arrayWithCapacity:[postsFromResponsse count]];
        for (NSDictionary *attributes in postsFromResponsse) {
            Post *post = [[Post alloc] initWithAttributes:attributes];
            [mutablePosts addObject:post];
        }
        
        if (block) {
            block([NSArray arrayWithArray:mutablePosts], nil);
        }

        } failure:^(NSError *error) {
            if (block) {
                block([NSArray array], error);
            }
           NSLog(@"请求失败");
    }];
}

- (void)cancel
{
    
}
@end
